﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Component;

public partial class BackEnd_News : System.Web.UI.UserControl
{
    public string _UrlModify = BackEndQuery._AdminQuery + BackEndQuery._NewsM + "&" + ConstString._idcat + ConstString._dauBang;
    public string _Session = "";
    string _panrent = "";
    public string _rPanrent = "";
    public string _urlChung = "";
    public string _Url = BackEndQuery._AdminQuery + BackEndQuery._CategoryL;
    public string _Urlnl = BackEndQuery._AdminQuery + BackEndQuery._NewsL + "&" + ConstString._Parent + ConstString._dauBang;
    public string _UrlCS = BackEndQuery._AdminQuery + BackEndQuery._CategorySub + "&" + ConstString._Parent + ConstString._dauBang;
    protected void Page_Load(object sender, EventArgs e)
    {
        _panrent = Request.QueryString[ConstString._Parent] ?? "";
        _rPanrent = Request.QueryString[ConstString._RParent] ?? "";
        if (_panrent == "")
        {
            _panrent = _rPanrent;
            
        }
        if (_panrent == "")
        {
            Load_Data();
        }
        else
            Load_Data1();
        if (_panrent == "23")
        {
            _urlChung = "<a href=\"" + _Url + "\">Danh Mục" + "</a>" + " -> " + "<a href=\"" + _UrlCS + "6" + "\">Tin Tức" + "</a>" + " -> " + "<a href=\"" + _Urlnl + _panrent + "\">Tin Tức Nội Bộ" + "</a>";
            
        }
        if (_panrent == "24")
        {
            _urlChung = "<a href=\"" + _Url + "\">Danh Mục" + "</a>" + " -> " + "<a href=\"" + _UrlCS + "6" + "\">Tin Tức" + "</a>" + " -> " + "<a href=\"" + _Urlnl + _panrent + "\">Tin Tức Tổng Hợp" + "</a>";
        }
        if (_panrent == "1")
        {
            _urlChung = "<a href=\"" + _Url + "\">Danh Mục" + "</a>" + " -> " + "<a href=\"" + _Urlnl + _panrent + "\">Trang Chủ" + "</a>";
        }
        if (_panrent == "2")
        {
            _urlChung = "<a href=\"" + _Url + "\">Danh Mục" + "</a>" + " -> " + "<a href=\"" + _Urlnl + _panrent + "\">GIỚI THIỆU" + "</a>";
        }
        if (_panrent == "3")
        {
            _urlChung = "<a href=\"" + _Url + "\">Danh Mục" + "</a>" + " -> " + "<a href=\"" + _Urlnl + _panrent + "\">DỊCH VỤ" + "</a>";
        }
        if (_panrent == "4")
        {
            _urlChung = "<a href=\"" + _Url + "\">Danh Mục" + "</a>" + " -> " + "<a href=\"" + _Urlnl + _panrent + "\">KHÁCH HÀNG" + "</a>";
        }
        if (_panrent == "5")
        {
            _urlChung = "<a href=\"" + _Url + "\">Danh Mục" + "</a>" + " -> " + "<a href=\"" + _Urlnl + _panrent + "\">TÀI LIỆU" + "</a>";
        }
        if (_panrent == "6")
        {
            _urlChung = "<a href=\"" + _Url + "\">Danh Mục" + "</a>" + " -> " + "<a href=\"" + _Urlnl + _panrent + "\">TIN TỨC" + "</a>";
        }
        if (_panrent == "7")
        {
            _urlChung = "<a href=\"" + _Url + "\">Danh Mục" + "</a>" + " -> " + "<a href=\"" + _Urlnl + _panrent + "\">TUYỂN DỤNG" + "</a>";
        }
        if (_panrent == "8")
        {
            _urlChung = "<a href=\"" + _Url + "\">Danh Mục" + "</a>" + " -> " + "<a href=\"" + _Urlnl + _panrent + "\">LIÊN HỆ" + "</a>";
        }
        
        //Session["cat"] = _Session = "Danh Mục -> <a href=\"" + Request.RawUrl + "\" >Tin Tức</a>";
        grid.RowCancelingEdit += delegate { Response.Redirect(Request.RawUrl); };
        grid.RowCommand += new GridViewCommandEventHandler(grid_RowCommand);
        //addNew.Visible = false;
    }

    void grid_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "lbtUpdate")
        {
            Response.Redirect(_UrlModify + e.CommandArgument.ToString());
        }
        if (e.CommandName == "lbtNewSub")
        {
            Response.Redirect(_UrlModify + e.CommandArgument.ToString());
        }
    }

    private void Load_Data()
    {
        
        grid.DataKeyNames = new string[] { ColumNews._Id };
        grid.DataSource = NewsDB.GetAll();
        grid.DataBind();

    }
    private void Load_Data1()
    {

        grid.DataKeyNames = new string[] { ColumNews._Id };
        grid.DataSource = NewsDB.GetByParent(int.Parse(_panrent == "" ? "0" : _panrent));
        grid.DataBind();

    }

    protected void grid_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        object id = grid.DataKeys[e.RowIndex].Value;
        TextBox txtTitile = (TextBox)grid.Rows[e.RowIndex].FindControl("txtTitile");
        TextBox txtSummary = (TextBox)grid.Rows[e.RowIndex].FindControl("txtSummary");        
        NewsInfo cat = NewsDB.GetById((int)id);
        cat.Titile = txtTitile.Text;
        cat.Summary = txtSummary.Text;
        Response.Redirect(Request.RawUrl);
    }
    protected void grid_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grid.EditIndex = e.NewEditIndex;
        Load_Data1();
    }
    protected void grid_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            object id = grid.DataKeys[e.RowIndex].Value;
            if (NewsDB.Delete((int)id))
                this.labMessage.Text = ConstString._xoaTC;
            else
                labMessage.Text = ConstString._xoaTB;
        }
        catch { }
        Load_Data();
    }
    protected void grid_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grid.PageIndex = e.NewPageIndex;
        Load_Data1();
    }

    protected void click(object sender, EventArgs e)
    {
        //if (addNew.Visible == false)
        //{
        //    addNew.Visible = true;
        //    this.lbtnAdd.Text = "đóng lại";
        //}
        //else
        //{
        //    addNew.Visible = false;
        //    this.lbtnAdd.Text = "Thêm một liên hệ mới";
        //    Page_Load(sender, e);
        //}

    }
}
